﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.Utilities;
using NetDon.WebControls;
using NetDon.eBoss.Data.CollectionClasses;
using NetDon.eBoss.Data.EntityClasses;
using NetDon.eBoss.Data.HelperClasses;
using NetDon.eBoss.Portal.HtmlTool;

namespace NetDon.OA.Portal.HtmlTool
{
    public partial class BatchDescriptionEdit : NetDonPage
    {
        protected IList<int?> Ids
        {
            get
            {
                if (string.IsNullOrWhiteSpace(this.fieldIds.Value))
                    return new List<int?>();
                return this.fieldIds.Value.Split(new char[] { ',' }).Select(id => id.ToInt32()).ToList();

            }
        }

        protected HtmltoolaccountEntity DefaultAccount
        {
            get
            {
                return new HtmlToolAccountService().GetHtmlToolDefaultAccount();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CancelButton = btnReturn;


            }
        }

        public void ddlHtmlToolCategory_SelectedIndexChanged(object sender, EventArgs e)
        {

            LoadHtmlToolStyles();
            LoadVocabulary();

        }

        public void ddlHtmlToolTrademark_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(ddlHtmlToolTrademark.SelectedValue))
            {
                txtMarket.Text = new HtmltooltrademarksettingEntity(ddlHtmlToolTrademark.SelectedValue.ToInt32().Value).Market;
            }


        }




        private void LoadHtmlToolStyles()
        {

            if (!string.IsNullOrWhiteSpace(ddlHtmlToolCategory.SelectedValue))
            {

                this.ddlHtmlToolStyle.DataSource = new HtmlToolDataSource().RetrieveHtmlToolStyle(Convert.ToInt32(ddlHtmlToolCategory.SelectedValue));
                this.ddlHtmlToolStyle.DataBind();
                ddlHtmlToolStyle.InsertEmptyItem(ListEmptyType.All);
                this.lblTitleVocabulary.Text = Server.HtmlEncode(GetVocabulary(true));
            }
        }

        private void LoadVocabulary()
        {
            if (!string.IsNullOrWhiteSpace(ddlHtmlToolCategory.SelectedValue))
            {

                this.lblTitleVocabulary.Text = Server.HtmlEncode(GetVocabulary(true));
                this.labelVocabularyMaterial.Text = Server.HtmlEncode(GetVocabulary(false));
            }

        }

        private void LoadDropDownLists(ProductgroupEntity entity)
        {
            if (entity != null)
            {
                LoadeBayColor(entity.Color);
                if (entity.Categoryruleid.HasValue)
                    ddlHtmlToolCategory.SelectedValue = entity.Categoryruleid.Value.ToString();
                //if (entity.Colorid.HasValue)
                //    ddlHtmlToolColor.SelectedValue = entity.Colorid.Value.ToString();
                if (entity.Trademarkid.HasValue)
                    ddlHtmlToolTrademark.SelectedValue = entity.Trademarkid.Value.ToString();
                LoadHtmlToolStyles();

                if (entity.Styleid.HasValue)
                    ddlHtmlToolStyle.SelectedValue = entity.Styleid.Value.ToString();
            }
        }

        private void LoadeBayColor(string color)
        {
            var service = new HtmlToolService();
            var entity = service.GetTitleColorSetting(color);
            if (entity != null)
            {
                if (entity.Htmltoolcolorid.HasValue)
                {
                    txteBayColor.Text = new HtmltoolcolorEntity(entity.Htmltoolcolorid.Value).Color;
                }
            }
        }





        protected void btnSave_Click(object sender, EventArgs e)
        {
            foreach (var id in Ids)
            {
                if (id.HasValue)
                {
                    var entity = new ProductgroupEntity(id.Value);

                    entity.Categoryruleid = ddlHtmlToolCategory.SelectedValue.ToInt32();
                    entity.Trademarkid = ddlHtmlToolTrademark.SelectedValue.ToInt32();
                    entity.Market = txtMarket.Text.Trim();

                    entity.Specifics = txtSpecifics.Text.Trim();
                    entity.Retailprice = txtRetailPrice.Text;
                    entity.Styleid = ddlHtmlToolStyle.SelectedValue.ToInt32();
                    //  entity.Colorid = ddlHtmlToolColor.SelectedValue.ToInt32();
                    entity.Material = txtMaterial.Text.Trim();
                    entity.Comment = txtComment.Text.Trim();
                    entity.Description = txtDescription.Text.Trim();


                    entity.Sizetable = txtSizetable.Text.Trim();
                    entity.Englishtitle = txtEnglishtitle.Text.Trim();

                    entity.Save();
                }
            }
            CloseWindow(true);


        }

        private void LoadInitialView()
        {
            if (Ids.Any())
            {
                foreach (var id in Ids)
                {
                    if (id.HasValue)
                    {
                        var group = new ProductgroupEntity();
                        group.FetchUsingPK(id.Value);

                        panelCustomInfo.DataSource = group;
                        panelCustomInfo.DataBind();
                        LoadDropDownLists(group);

                        break;


                    }
                }
            }


            LoadVocabulary();
        }

        protected void btnReLoadIds_Click(object sender, EventArgs e)
        {
            LoadInitialView();
            btnReLoadIds.Enabled = false;
        }


        public string GetVocabulary(bool isTitle)
        {
            var vocabulary = string.Empty;
            if (!string.IsNullOrWhiteSpace(ddlHtmlToolCategory.SelectedValue))
            {
                var collection = new HtmltoolvocabularyCollection();
                collection.GetMulti(HtmltoolvocabularyFields.Categoryruleid == ddlHtmlToolCategory.SelectedValue.ToInt32().Value & HtmltoolvocabularyFields.Sitecode == new HtmltoolaccountEntity(DefaultAccount.Id).Sitecode);
                var vocabularyEntity = collection.FirstOrDefault();
                if (vocabularyEntity != null)
                {


                    if (isTitle)
                    {
                        vocabulary = vocabularyEntity.Titleword;
                    }
                    else
                    {
                        vocabulary = vocabularyEntity.Materialword;

                    }
                    vocabulary = vocabulary.Replace(@"\n", "<Br/>").Replace(@"\r", "<Br/>");
                    vocabulary = vocabulary.Replace(@"\t", "<Br/>");

                }
            }
            return vocabulary;
        }



    }
}